//var map;
var marker = new Array();
;(function($) {
	  map = new AMap.Map('container', {
         resizeEnable: true,
        // center:[116.480983, 39.989628],
         zoom:12
      });
	
/*	
	map.on('click', function(e) {
		console.log(e);
		$(".address_longitude").val(e.lnglat.getLng());
		$(".address_latitude").val(e.lnglat.getLat());
		//获取详细地址
		getAddressDetail(e.lnglat.getLat(),e.lnglat.getLng());
		map.clearMap();
		var lnglatXY=[e.lnglat.getLng(),e.lnglat.getLat()];
		var marker_obj = new AMap.Marker({  //加点
	            map: map,
	            title:e.lnglat.getLng() +","+e.lnglat.getLat()
	            //position: lnglatXY
	        });
		marker_obj.setPosition(lnglatXY);
		
	});*/
})(jQuery)



/*function getLongitudeAndLatitude(address){
		//var address = $(obj).prev().val();
		if(address==null||address==""){
			LQWarn("请先输入地址");
			return false;
		}
		placeSearch(address);
}*/

function getAddressDetail(lat,lng){
	var pos ='http://restapi.amap.com/v3/geocode/regeo?key=1b76797c593dff1bd16b6c80dc986b6d&'
		+'location='+lng+','+lat
		+'&radius=1000';
	$.ajax({
		url: pos, 
		type: 'get',
		async: true,
		error: function(){LQWarn('请求失败,请稍后重试');}, 
		success: function(result){
			console.log(result);
			console.log(result.regeocode.addressComponent.province);
			var addr = result.regeocode.addressComponent;
			
			$("input[name='province']").val(addr.province);
			$("input[name='city']").val(addr.city);
			$("input[name='area']").val(addr.district);
			$("input[name='address']").val(result.regeocode.formatted_address);
		}
	})
}
	 
	//基本地图加载
	function placeSearch(address){
		map.clearMap();
	    var MSearch;
	  	AMap.service(["AMap.PlaceSearch"], function() {

	        MSearch = new AMap.PlaceSearch({ //构造地点查询类
	            pageSize:10,
	            pageIndex:1
	        });
	        //关键字查询
	        MSearch.search(address, function(status, result){
	        	if(status === 'complete' && result.info === 'OK'){
	        		var resultStr = "";
				    var poiArr = result.poiList.pois;
				    var resultCount = poiArr.length;
				    var index = 0;
				    if(resultCount>0){
				    	index = 0;
				    }else{
				    	//没有数据
				    	return ;
				    }
					var d = poiArr[index];
					console.log(d);
					console.log(result);
		    		var lngX = d.location.getLng();
					var latY = d.location.getLat();
					$(".address_longitude").val(lngX);
					$(".address_latitude").val(latY);
		            addmarker(index, d);
		            map.setFitView();
				  	
	        	}else{
	        		LQWarn("未搜索到该地址");
	        		return false;
	        	}
	        }); 
	    });
	}

	//添加marker&infowindow   
	function addmarker(i, d) {
		var lngX;
		var latY;
		
	    lngX = d.location.getLng();
	    latY = d.location.getLat();
	    var markerOption = {
	        map:map,
	        icon:"http://webapi.amap.com/images/" + (i + 1) + ".png",
	        position:new AMap.LngLat(lngX, latY),
	        topWhenMouseOver:true,
	        title:lngX +","+latY
	    };
	   
	    
	    var mar = new AMap.Marker(markerOption);         
	    marker.push(new AMap.LngLat(lngX, latY));
	 
	    var infoWindow = new AMap.InfoWindow({
	        size:new AMap.Size(300, 0),
	        autoMove:true,
	        offset:new AMap.Pixel(0,-20)
	    });
	}

function regeocoder(lnglatXY) {  //逆地理编码 经纬度定位地图
    	AMap.service('AMap.Geocoder',function(){//回调函数
            //实例化Geocoder
    		geocoder = new AMap.Geocoder({
	            radius: 1000,
	            extensions: "all"
	        }); 
        });
        geocoder.getAddress(lnglatXY, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                
            }
        });
        var marker = new AMap.Marker({  //加点
            map: map,
            //position: lnglatXY
        });
        marker.setPosition(lnglatXY);
        map.setFitView(); 
     
       
}
	
	
